package org.xbill.DNS;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.xbill.DNS.utils.base16;

public class APLRecord extends Record
{
  private static final long serialVersionUID = 1366655048L;
  private List elements;

  public APLRecord(Name paramName, int paramInt, long paramLong, List paramList)
  {
    super(paramName, 42, paramInt, paramLong);
    this.elements = new ArrayList(paramList.size());
    Iterator localIterator = paramList.iterator();
    while (true)
    {
      if (!(localIterator.hasNext()))
        return;
      Object localObject = localIterator.next();
      if (!(localObject instanceof Element))
        throw new IllegalArgumentException("illegal element");
      Element localElement = (Element)localObject;
      if ((localElement.family != 1) && (localElement.family != 2))
        throw new IllegalArgumentException("unknown family");
      this.elements.add(localElement);
    }
  }

  private static int addressLength(byte[] paramArrayOfByte)
  {
    int i = paramArrayOfByte.length - 1;
    while (true)
    {
      int j;
      if (i < 0)
        j = 0;
      while (true)
      {
        return j;
        if (paramArrayOfByte[i] == 0)
          break;
        j = i + 1;
      }
      --i;
    }
  }

  private static byte[] parseAddress(byte[] paramArrayOfByte, int paramInt)
    throws WireParseException
  {
    Object localObject;
    if (paramArrayOfByte.length > paramInt)
      throw new WireParseException("invalid address length");
    if (paramArrayOfByte.length == paramInt)
      localObject = paramArrayOfByte;
    while (true)
    {
      return localObject;
      byte[] arrayOfByte = new byte[paramInt];
      System.arraycopy(paramArrayOfByte, 0, arrayOfByte, 0, paramArrayOfByte.length);
      localObject = arrayOfByte;
    }
  }

  private static boolean validatePrefixLength(int paramInt1, int paramInt2)
  {
    int i;
    if ((paramInt2 < 0) || (paramInt2 >= 256))
      i = 0;
    while (true)
    {
      while (true)
      {
        return i;
        if ((((paramInt1 != 1) || (paramInt2 <= 32))) && (((paramInt1 != 2) || (paramInt2 <= 128))))
          break;
        i = 0;
      }
      i = 1;
    }
  }

  public List getElements()
  {
    return this.elements;
  }

  Record getObject()
  {
    return new APLRecord();
  }

  void rdataFromString(Tokenizer paramTokenizer, Name paramName)
    throws IOException
  {
    this.elements = new ArrayList(1);
    while (true)
    {
      int l;
      int i1;
      Tokenizer.Token localToken = paramTokenizer.get();
      if (!(localToken.isString()))
      {
        paramTokenizer.unget();
        return;
      }
      boolean bool = false;
      String str1 = localToken.value;
      int i = 0;
      if (str1.startsWith("!"))
      {
        bool = true;
        i = 1;
      }
      int j = str1.indexOf(58, i);
      if (j < 0)
        throw paramTokenizer.exception("invalid address prefix element");
      int k = str1.indexOf(47, j);
      if (k < 0)
        throw paramTokenizer.exception("invalid address prefix element");
      String str2 = str1.substring(i, j);
      String str3 = str1.substring(j + 1, k);
      String str4 = str1.substring(k + 1);
      try
      {
        l = Integer.parseInt(str2);
        if ((l != 1) && (l != 2))
          throw paramTokenizer.exception("unknown family");
      }
      catch (NumberFormatException localNumberFormatException1)
      {
        throw paramTokenizer.exception("invalid family");
      }
      try
      {
        i1 = Integer.parseInt(str4);
        if (!(validatePrefixLength(l, i1)))
          throw paramTokenizer.exception("invalid prefix length");
      }
      catch (NumberFormatException localNumberFormatException2)
      {
        throw paramTokenizer.exception("invalid prefix length");
      }
      byte[] arrayOfByte = Address.toByteArray(str3, l);
      if (arrayOfByte == null)
        throw paramTokenizer.exception("invalid IP address " + str3);
      InetAddress localInetAddress = InetAddress.getByAddress(arrayOfByte);
      List localList = this.elements;
      Element localElement = new Element(bool, localInetAddress, i1);
      localList.add(localElement);
    }
  }

  void rrFromWire(DNSInput paramDNSInput)
    throws IOException
  {
    boolean bool;
    byte[] arrayOfByte;
    Element localElement;
    this.elements = new ArrayList(1);
    if (paramDNSInput.remaining() == 0)
      return;
    int i = paramDNSInput.readU16();
    int j = paramDNSInput.readU8();
    int k = paramDNSInput.readU8();
    if ((k & 0x80) != 0)
      bool = true;
    while (true)
    {
      arrayOfByte = paramDNSInput.readByteArray(k & 0xFFFFFF7F);
      if (validatePrefixLength(i, j))
        break;
      throw new WireParseException("invalid prefix length");
      bool = false;
    }
    if ((i == 1) || (i == 2))
      localElement = new Element(bool, InetAddress.getByAddress(parseAddress(arrayOfByte, Address.addressLength(i))), j);
    while (true)
    {
      while (true)
        this.elements.add(localElement);
      localElement = new Element(i, bool, arrayOfByte, j, null);
    }
  }

  String rrToString()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    Iterator localIterator = this.elements.iterator();
    while (true)
    {
      do
      {
        if (!(localIterator.hasNext()))
          return localStringBuffer.toString();
        localStringBuffer.append((Element)localIterator.next());
      }
      while (!(localIterator.hasNext()));
      localStringBuffer.append(" ");
    }
  }

  void rrToWire(DNSOutput paramDNSOutput, Compression paramCompression, boolean paramBoolean)
  {
    byte[] arrayOfByte;
    int i;
    Iterator localIterator = this.elements.iterator();
    if (!(localIterator.hasNext()))
      return;
    Element localElement = (Element)localIterator.next();
    if ((localElement.family == 1) || (localElement.family == 2))
    {
      arrayOfByte = ((InetAddress)localElement.address).getAddress();
      i = addressLength(arrayOfByte);
    }
    while (true)
    {
      while (true)
      {
        int j = i;
        if (localElement.negative)
          j |= 128;
        paramDNSOutput.writeU16(localElement.family);
        paramDNSOutput.writeU8(localElement.prefixLength);
        paramDNSOutput.writeU8(j);
        paramDNSOutput.writeByteArray(arrayOfByte, 0, i);
      }
      arrayOfByte = (byte[])localElement.address;
      i = arrayOfByte.length;
    }
  }

  public static class Element
  {
    public final Object address;
    public final int family;
    public final boolean negative;
    public final int prefixLength;

    private Element(int paramInt1, boolean paramBoolean, Object paramObject, int paramInt2)
    {
      this.family = paramInt1;
      this.negative = paramBoolean;
      this.address = paramObject;
      this.prefixLength = paramInt2;
      if (!(APLRecord.access$0(paramInt1, paramInt2)))
        throw new IllegalArgumentException("invalid prefix length");
    }

    public Element(boolean paramBoolean, InetAddress paramInetAddress, int paramInt)
    {
      this(Address.familyOf(paramInetAddress), paramBoolean, paramInetAddress, paramInt);
    }

    public boolean equals(Object paramObject)
    {
      int i;
      if ((paramObject == null) || (!(paramObject instanceof Element)))
        i = 0;
      while (true)
      {
        while (true)
        {
          return i;
          Element localElement = (Element)paramObject;
          if ((this.family != localElement.family) || (this.negative != localElement.negative) || (this.prefixLength != localElement.prefixLength) || (!(this.address.equals(localElement.address))))
            break;
          i = 1;
        }
        i = 0;
      }
    }

    public int hashCode()
    {
      int j;
      int i = this.address.hashCode() + this.prefixLength;
      if (this.negative)
        j = 1;
      while (true)
      {
        return (i + j);
        j = 0;
      }
    }

    public String toString()
    {
      StringBuffer localStringBuffer = new StringBuffer();
      if (this.negative)
        localStringBuffer.append("!");
      localStringBuffer.append(this.family);
      localStringBuffer.append(":");
      if ((this.family == 1) || (this.family == 2))
        localStringBuffer.append(((InetAddress)this.address).getHostAddress());
      while (true)
      {
        localStringBuffer.append("/");
        localStringBuffer.append(this.prefixLength);
        return localStringBuffer.toString();
        localStringBuffer.append(base16.toString((byte[])this.address));
      }
    }
  }
}